package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.UserLike;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.ILikeService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private ITagService tagService;

    /**
     *给某篇游记添加评论
     * @return
     */
    @PostMapping(value = "/strategies/{strategy.id}/comments/{user.id}")
    public JSONResult saveComment(String imgUrls, Long[] tagIds,
                                  String myTags, StrategyComment strategyComment){
        JSONResult result = new JSONResult();
        //1.保存攻略评论
        try {
            commentService.save(strategyComment);
        }catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        //2.保存评论关联的标签
        try{
            //保存已定义标签
            commentService.setTags(strategyComment.getId(),tagIds);
            //保存自定义标签
            String[] tagNames = myTags.split("\\s+");
            for (String tagName: tagNames) {
                Tag tag = new Tag();
                tag.setName(tagName);
                tag.setType(Tag.TYPE_CUSTOMIZED);
                tag.setCommentId(strategyComment.getId());
                tagService.save(tag);
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    /**
     * 根据攻略的id查询对应的评论
     * @param qo
     * @return
     */
    @GetMapping(value = "/strategies/comments/{strategyId}")
    public Object getDetailById(StrategyCommentQueryObject qo) {
        PageInfo pageInfo =  commentService.query(qo);
        return pageInfo;
    }

    /**
     * 根据id查询攻略的评论
     * @param id
     * @return
     */
    @GetMapping(value = "/strategies/{id}/comments")
    public Object getDetailById(@PathVariable Long id) {
        StrategyComment strategyComment = commentService.getById(id);
        return strategyComment;
    }

    /**
     * 根据用户id查询他的攻略评论
     * @param id
     * @return
     */
    @GetMapping(value = "/strategies/users/{id}/comments")
    public Object listByUserId(@PathVariable Long id) {
        List<StrategyComment> strategyCommentList = commentService.listByUserId(id);
        return strategyCommentList;
    }


    @Autowired
    private ILikeService likeService;


    /**
     * 新增或删除评论的点赞
     * @param
     * @return
     */
    @PostMapping(value = "/strategies/comments/likes")
    public Object updateLikes(UserLike userLike) {
        Map data = likeService.updateCommnetLikes(userLike);
        return data;//返回这条评论的最新点赞数
    }
}
